package com.pioneer.swing.model;

import com.pioneer.dao.ManifestDAO;
import com.pioneer.dao.DataAccessException;
import com.pioneer.beans.ContainerContent;
import com.pioneer.beans.Container;
import com.pioneer.beans.Key;
import com.pioneer.beans.ContainerKey;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;
import java.util.List;
import java.util.Iterator;

/**
 * Title:        Pioneer Metals Application
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      Pioneer Metals
 * @author Gary Chen
 * @version 1.0
 */

public class ContainerEntryTable extends DefaultTableModel {

	protected ManifestDAO dao;
	protected String containerId;
	protected String jobNum;

	// column numbers
	public static final int DESCRIPTION=0;
	public static final int PACKING=1;
	public static final int FOB_C_NUM=2;
	public static final int WEIGHT=3;
	public static final int COST=4;
	public static final int FOB_AMOUNT=5;

    public ContainerEntryTable(ManifestDAO dao,Key key) {
		ContainerKey containerKey=(ContainerKey)key;
		String jobNum=containerKey.getJobNum();
		String containerId=containerKey.getId();
		columnIdentifiers=new Vector();
		columnIdentifiers.add(TableConstants.DESCRIPTION);
		columnIdentifiers.add(TableConstants.PACKING);
		//columnIdentifiers.add(TableConstants.FOB);
		columnIdentifiers.add(TableConstants.FOB_C_NUM);
		columnIdentifiers.add(TableConstants.WEIGHT);
		columnIdentifiers.add(TableConstants.COST);
		columnIdentifiers.add(TableConstants.FOB_AMOUNT);

		this.dao=dao;
		this.containerId=containerId;
		this.jobNum=jobNum;
		if (containerId!=null) refresh();
    }


	public Object getValueAt(int row, int column) {
		Object objFound=super.getValueAt(row,column);
		if (objFound==null) {
			// initialize them
			if ((column==FOB_AMOUNT)||
			(column==FOB_C_NUM)||
			(column==WEIGHT)) {
				// We will not use float because it makes data entry slower
				//return new Float(0);
				return new String();
			}
			else {
				return new String();
			}
		}
		else return objFound;
	}


	public void refresh()
	{
		try
		{
			List containerContent=dao.getContainerContents(jobNum,containerId);
			// all rows
			dataVector=new Vector();
			Iterator rows=containerContent.iterator();
			while (rows.hasNext())
			{
				ContainerContent content=(ContainerContent)rows.next();
				Vector row=new Vector();
				row.add(content.getDescription());
				row.add(content.getRemarks());
				//row.add(content.getFob());
				row.add(new Float(content.getFobCNumber()));
				row.add(new Float(content.getWeight()));
				row.add(new Float(content.getCost()));
				row.add(new Float(content.getFobAmount()));
				dataVector.add(row);
			}
		}
		catch (DataAccessException ex)
		{
			ex.printStackTrace(System.err);
		}
	}

    public static void main(String[] args) {
        //ContainerEntryTable allManifestsTable1 = new ContainerEntryTable();
    }
}